<?php
/**
 * 设置分组
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Page;
class SettingGroupController extends CommonController{
	private $msg=array();
	private $Model;
	function _initialize(){
		parent::_initialize();
		$this->Model=M('SettingGroup');
	}
	public function main(){
		$this->redirect('index');
	}
	public function index(){
		if(!$this->checkPermission('SettingGroup')) $this->jump();
		$count	=	$this->Model->count();
		$Page	=	new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$show=$Page->show();
		$settinggroups=$this->Model->order('sort_order ASC')->limit($Page->firstRow,$Page->listRows)->select();
		$list=array();
		$language_id=get_default_lng_id();
		foreach ($settinggroups as $v) {
			$text=json_decode($v['text'],true);
			$list[]=array(
				'setting_group_id'	=>	$v['setting_group_id'],
				'title'				=>	$text[$language_id]['title'],
				'sort_order'		=>	$v['sort_order']
				);
		}
		$url['p']=I('get.p','');
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->display();
	}
	public function oper($setting_group_id=0){

		if(!$setting_group_id && !$this->checkPermission('AddSettingGroup')) $this->jump();
		$map=array('setting_group_id'=>$setting_group_id);
		$setting_group=$this->Model->where($map)->find();
		if($setting_group_id && empty($setting_group)) $this->jump(L('error_404'));
		if($setting_group) $setting_group['text']=json_decode($setting_group['text'],true);
		if(IS_POST){
			$setting_group=I('post.');
			if($this->validateForm()){
				$setting_group['text']=json_encode($setting_group['text']);
				if($setting_group_id){
					$this->Model->where($map)->save($setting_group);
				}else{
					$this->Model->add($setting_group);
				}
				session('success',$setting_group_id?L('success_edit'):L('success_add'));
				$url['p']=I('get.p','');
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$languages=get_langs();
		$this->assign('setting_group',$setting_group);
		$this->assign('languages',$languages);
		$this->assign('msg',$this->msg);
		$this->display();
	}
	public function del(){
		if($this->checkPermission('DeleteSettingGroup')){
			$ids=I('post.selected');
			$count=0;
			if($this->validateDelete()){
				$map=array('setting_group_id'=>array('IN',$ids));
				$count=$this->Model->where($map)->delete();
				session('success',L('success_delete',array('count'=>$count)));
			}else{
				session('danger',$this->msg['warning']);
			}
		}else{
			session('danger',L('error_not_permission'));
		}
		$url['p']=I('get.p');
		$this->redirect('index',$url);
	}
	private function validateForm(){
		$setting_group_id=I('get.setting_group_id',0);
		if($setting_group_id && !$this->checkPermission('EditSettingGroup')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		foreach($data['text'] as $language_id=>$v){
			if(mb_strlen($v['title'])<2 || mb_strlen($v['title'])>50) $this->msg['title'][$language_id]=L('error_title');
		}
		return !$this->msg;
	}
	private function validateDelete(){
		$ids=I('post.selected');
		$map=array('setting_group_id'=>array('IN',$ids));
		if(M('Setting')->where($map)->find()){
			$this->msg['warning']=L('error_delete');
		}
		return !$this->msg;
	}
}